﻿using System.ComponentModel.Composition;
using Microsoft.Practices.Unity;
using TorchView.Composition;
using TorchView.Extensibility;
using TorchView.Game;
using TorchView.Windows;
using TorchView.SharedStashEditor.ViewModels;
using TorchView.SharedStashEditor.Views;
using System.IO;
using TorchView.SharedStashEditor.Models;
using TorchView.GameContent.Effects;
using TorchView.GameContent.Affixes;

namespace TorchView.SharedStashEditor
{
    [Export(typeof(IShellPlugin))]
    public class SharedStashEditorPlugin : IShellPlugin
    {
        private IUnityContainer _Container;

        [Import(typeof(ITorchViewShell))]
        public ITorchViewShell Shell { get; set; }

        private void EnsureInitialized()
        {
            if (_Container == null)
            {
                _Container = Shell == null || Shell.GlobalContainer == null ? new UnityContainer() : Shell.GlobalContainer.CreateChildContainer();

                _Container.RegisterInstance<ItemCollection>(ItemCollection.DefaultCollection, new ContainerControlledLifetimeManager());
                _Container.RegisterInstance<EffectList>(EffectList.DefaultList, new ContainerControlledLifetimeManager());
                _Container.RegisterInstance<AffixItemList>(AffixItemList.DefaultList, new ContainerControlledLifetimeManager());

                _Container.RegisterType<IAddItemViewModel, AddItemViewModel>();
                _Container.RegisterType<IDialogView<IAddItemViewModel>, AddItemView>();

                _Container.RegisterType<IAddEffectViewModel, AddEffectViewModel>();
                _Container.RegisterType<IDialogView<IAddEffectViewModel>, AddEffectView>();

                _Container.RegisterType<IAddAffixViewModel, AddAffixViewModel>();
                _Container.RegisterType<IDialogView<IAddAffixViewModel>, AddAffixView>();

                _Container.RegisterType<IEditEffectViewModel, EditEffectViewModel>();
                _Container.RegisterType<IDialogView<IEditEffectViewModel>, EditEffectView>();

                _Container.RegisterType<IEditDamageBonusViewModel, EditDamageBonusViewModel>();
                _Container.RegisterType<IDialogView<IEditDamageBonusViewModel>, EditDamageBonusView>();

                _Container.RegisterType<ISharedStashViewModel, SharedStashViewModel>(new ContainerControlledLifetimeManager());
                _Container.RegisterType<IView<ISharedStashViewModel>, SharedStashView>(new ContainerControlledLifetimeManager());

                Container = _Container;
            }
        }

        public static IUnityContainer Container
        {
            get;
            private set;          
        }

        #region IShellPlugin Members
        public string Name
        {
            get { return "Shared Stash Editor"; }
        }

        public object MainView
        {
            get
            {
                EnsureInitialized();
                return _Container.Resolve<ISharedStashViewModel>().View;
            }
        }
        #endregion
    }
}
